﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Possan.ZipFolders
{

    public class Folder
    {
        string _zippath;

        public string ZipFilePath
        {
            get { return _zippath; }
            set { _zippath = value; }
        }
        string _urlmatch;

        public string UrlMatch
        {
            get { return _urlmatch; }
            set { _urlmatch = value; }
        }
        string _ziproot;

        public string ZipBasePath
        {
            get { return _ziproot; }
            set { _ziproot = value; }
        }

        int _cachetime = 0;

        public int CacheDuration
        {
            get { return _cachetime; }
            set { _cachetime = value; }
        }

        public Folder()
        {
            ZipFilePath = "";
            ZipBasePath = "/";
            UrlMatch = "";
            CacheDuration = 24 * 60 * 60 * 30; // cacha i 30 dagar
        }

        public static Folder Parse(XmlNode node)
        {
            Folder f = new Folder();

            log4net.ILog l = log4net.LogManager.GetLogger("Possan.ZipFolders");
            l.Debug("Parsing folder: " + node.OuterXml);

            if (node.Attributes["Url"] != null)
            {
                f.UrlMatch = node.Attributes["Url"].Value;
            }

            if (node.Attributes["ZipFile"] != null)
            {
                f.ZipFilePath = node.Attributes["ZipFile"].Value;
            }

            if (node.Attributes["ZipBase"] != null)
            {
                f.ZipBasePath = node.Attributes["ZipBase"].Value;
            }

            if (node.Attributes["Cache"] != null)
            {
                int i = 0;
                if (int.TryParse(node.Attributes["Cache"].Value, out i))
                    f.CacheDuration = i;
            }

            if (f.UrlMatch != "" && f.ZipFilePath != "")
                return f;

            return null;
        }
    }
}
